package com.burger.counter.servlets;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.burger.counter.beans.Item;
import com.burger.counter.beans.ShoppingCart;
import com.burger.counter.beans.User;
import com.burger.counter.business.UserBusiness;

/**
 * Servlet implementation class ReceiptServlet
 */
public class ReceiptServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReceiptServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Date in servlet : " + request.getParameter("pickUpDate"));
		System.out.println("Time in Servlet: " + request.getParameter("pickUpTime"));
		String datetime=request.getParameter("pickUpDate") + " " + request.getParameter("pickUpTime");
		DateFormat dateformat = new SimpleDateFormat("mm-dd-yyyy hh:mm");
		try {
			Date date = dateformat.parse(datetime);
			System.out.println(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Item item=null;
		ShoppingCart cart=null;
		
		ArrayList<Item> itemList;
		
		HttpSession session = request.getSession(true);
		cart = (ShoppingCart) session.getAttribute("Cart");
		itemList=cart.getItemInCart();
		int userId = (Integer)session.getAttribute("UserId");
		
		UserBusiness userBI=new UserBusiness();
		
		User[] userDetails=userBI.getUserDetails(session.getAttribute("Email").toString());
		
		session.setAttribute("User", userDetails[0]);
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ViewReceipt.jsp");
		dispatcher.forward(request, response);
	}

}
